package com.itao.parse;

import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseResult;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.comments.Comment;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Optional;

public class CommentExtractor {

    public static void main(String[] args) throws IOException {
        // 读取源代码文件
        String property = System.getProperty("user.dir");
        String path = property + "/java-doc/";
        File sourceFile = new File(path + "src/main/java/com/itao/bean/Student.java");
        ParseResult<CompilationUnit> cu = new JavaParser().parse(new FileInputStream(sourceFile));
        Optional<CompilationUnit> result = cu.getResult();
        if (result.isEmpty()) {
            return;
        }
        CompilationUnit unit = result.get();
        // 遍历类和方法，提取注释
        for (ClassOrInterfaceDeclaration classDecl : unit.findAll(ClassOrInterfaceDeclaration.class)) {
            Optional<Comment> comment = classDecl.getComment();
            comment.ifPresent(value -> {
                String className = classDecl.getNameAsString();
                String content = value.getContent();
                if (content == null || content.isEmpty()) {
                    System.out.println("无类注释[" + className + "]");
                    return;
                }
                content = content.trim();
                if (content.contains("*")) {
                    Optional<String> first = content.lines().findFirst();
                    if (first.isPresent()) {
                        content = first.get().replace("*", "").trim();
                    }
                }
                if (content.isEmpty()) {
                    System.out.println("无类注释[" + className + "]");
                    return;
                }
                System.out.println("类注释[" + className + "]: " + content);
            });
            List<FieldDeclaration> fields = classDecl.getFields();
            for (FieldDeclaration field : fields) {
                field.getComment().ifPresent(value -> {
                    Optional<VariableDeclarator> optional = field.getVariables().getFirst();
                    String fieldName = null;
                    if (optional.isPresent()) {
                        fieldName = optional.get().getNameAsString();
                    }
                    String content = value.getContent();
                    if (content == null || content.isEmpty()) {
                        System.out.println("无字段注释[" + fieldName + "]");
                        return;
                    }
                    content = content.trim();
                    if (content.contains("*")) {
                        Optional<String> first = content.lines().findFirst();
                        if (first.isPresent()) {
                            content = first.get().replace("*", "").trim();
                        }
                    }
                    if (content.isEmpty()) {
                        System.out.println("无字段注释[" + fieldName + "]");
                        return;
                    }
                    System.out.println("字段注释[" + fieldName + "]: " + content);
                });
            }

            List<MethodDeclaration> methods = classDecl.getMethods();
            for (MethodDeclaration method : methods) {
                method.getComment().ifPresent(value -> {
                    String methodName = method.getNameAsString();
                    String content = value.getContent();
                    if (content == null || content.isEmpty()) {
                        System.out.println("无方法注释[" + methodName + "]");
                        return;
                    }
                    content = content.trim();
                    if (content.contains("*")) {
                        Optional<String> first = content.lines().findFirst();
                        if (first.isPresent()) {
                            content = first.get().replace("*", "").trim();
                        }
                    }
                    if (content.isEmpty()) {
                        System.out.println("无方法注释[" + methodName + "]");
                        return;
                    }
                    System.out.println("方法注释[" + methodName + "]: " + content);
                });
            }
        }
    }
}
